﻿package com.dcliment.jukebox.display.views
{
	import com.dcliment.display.gui.ISimpleButton;
	import com.greensock.TweenMax;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	/**
	 * HORIZONTAL version of NavBar
	 * 
	 * This is connected to a MovieClip inside the fla that should have 3 items on the stage
	 * - mArrowLeft
	 * - mArrowRight
	 * - mMask :: This is the mask for the buttons so that
	 *  
	 * @author dcliment
	 * 
	 */	
	public class NavViewV extends MovieClip implements INavView
	{
		private var _arrowL:MovieClip;
		private var _IArrowL:ISimpleButton;
		private var _arrowR:MovieClip;
		private var _IArrowR:ISimpleButton;
		private var _mask:MovieClip;
		// Appearence
		private var _spacing:Number;
		private var _n:int; // Number of visible buttons;
		private var _b0_x:Number;
		private var _b0_y:Number;
		//
		private var _buttonView:Sprite;
		// Tween
		private var tSpd:Number = 0.5;
		private var tEasting:String = "Quad.easeInOut";
		private var tArea:Number;
		private var tStartingPos:Number;
		private var tMove:Number;
		//
		private var buttons:Array = new Array();
		private var numOfMoves:int; 
		
		public function NavViewV()
		{
			_arrowL = this["mArrowLeft"];
			_arrowR = this["mArrowRight"];
			_mask 	= this["mMask"];
			
			// CHECKS AND BALANCES
			if(!_arrowL)
				throw new Error("NavView ERROR :: Linkage Name 'mArrowLeft' was not found in the movieclip");
			if(!_arrowR)
				throw new Error("NavView ERROR :: Linkage Name 'mArrowRight' was not found in the movieclip");
			if(!_mask)
				throw new Error("NavView ERROR :: Linkage Name 'mMask' was not found in the movieclip");
			
			_arrowL.addEventListener(MouseEvent.CLICK, moveRight, false, 0, true);
			_arrowL.buttonMode = true;
			_arrowR.addEventListener(MouseEvent.CLICK, moveLeft, false, 0, true);
			_arrowR.buttonMode = true;
			
			// INterfaces -- checks and balances
			try
			{
				_IArrowL = ISimpleButton(_arrowL);
			}
			catch(e:Error)
			{
				throw new Error("NavViewH ERROR :: mArrowLeft does not implement ISimpleButton");
			}
			
			try
			{
				_IArrowR = ISimpleButton(_arrowR);
			}
			catch(e:Error)
			{
				throw new Error("NavViewH ERROR :: mArrowRight does not implement ISimpleButton");
			}
			
			
			// BUTTON VIEW -- exactly where mask is
			_buttonView = new Sprite();
			_buttonView.x = _mask.x;
			_buttonView.y = _mask.y;
			_buttonView.mask = _mask;
			
			//
			tStartingPos = _buttonView.y;
			tArea = _mask.height;
			
			addChild(_buttonView);
		}
		/**
		 * 
		 * @param $spacing :: spacing between Buttons
		 * @param $n :: Number of visible Buttons
		 * 
		 */		
		public function init($spacing:Number, $n:int, $b0_x:Number=NaN,$b0_y:Number=NaN):void
		{
			
			_spacing = $spacing;
			_n = $n;
			_b0_x = $b0_x;
			_b0_y = $b0_y;
			
			
			
			
		}
		/**
		 * Places Button in correct place
		 * 
		 * @param b :: Button
		 * 
		 */		
		public function addButton(b:DisplayObject):void
		{
			//------------------------------------------------
			// CHECKs and balance for $number of visible buttons per avaailable area 
			if(buttons.length == 0)
			{
				var spaceLeft:Number = _mask.height - (b.height * _n);
				
				if(spaceLeft < 0)
					throw new Error("NavViewH ERROR :: the number of visible buttons set is to high. Please lower the <navButton numOfVisible''/> in XML or reduce size of buttons");
				
				// ----------------------------------------------
				// Calculates _spacing if it doesn't exist
				if(isNaN(_spacing)) 
				{
					_spacing = spaceLeft/_n;
				}
			}
			
			//--------------------------------
			// SPECIFIC TO FIRST NAV BUTTON
			if(buttons.length == 0)
			{
				if(isNaN(_b0_y)){
					b.y = (_spacing * 0.5); // take half of spacing so its equal on the other side
				}else{
					b.y = _b0_y;
				}
			}
			else
			{
				var b0:DisplayObject = buttons[buttons.length-1];
				
				b.y = b0.y + b0.height + _spacing;
			}
			//b.x = (b.height * n) + ($spacing * n);
			
			
			if(isNaN(_b0_x)){
				
				b.x = (_mask.width / 2) - (b.width / 2);
			}else{
				b.x= _b0_x;
				
			}
			
			
			_buttonView.addChild(b);
			
			buttons.push(b);
		
		}
		
		public function move($n:int):void
		{
			this.numOfMoves = $n;
			
			tMove = (tArea * numOfMoves);
			
			//trace("Move position = " + tMove);
		
			TweenMax.to(_buttonView, tSpd, {y:tStartingPos - tMove, ease:tEasting, onStart:checkArrows});
			
			
		}
		////////////////
		// MOVEMENT
		////////////////
		private function moveLeft(e:MouseEvent):void
		{
			this.numOfMoves++;
			move(this.numOfMoves);
			
		}
		private function moveRight(e:MouseEvent):void
		{
			this.numOfMoves--;
			move(this.numOfMoves);
			
		}
		private function checkArrows():void
		{
			
			var n:Number = _buttonView.width - tMove - _mask.width; // not sure how i got to this but it works
			
			
			if(tMove == 0)
			{
				_IArrowL.disable();
				
			}
			else
			{
				_IArrowL.enable();
			}
			
			if(n <= 0)
			{
				_IArrowR.disable();
			}
			else
			{
				_IArrowR.enable();
				
			}
			
		}
		
	}
}